"use strict";
var Premium = Premium || {};

Premium.creative = {
    init: function() {
        /* START OF CUSTOM JS */

        Premium.communicator.init(document.body.id === "body_expanded" ? undefined : 4);

        switch (document.body.id) {
            case "body_top":
                // TOP ---------------------------------------------------------------
                var videoEl = document.querySelector("VIDEO");
                var videoContainerEl = document.getElementById("video-container");

                Premium.video.switchOnScroll(videoContainerEl, 3);
                Premium.video.sync(videoEl, 1);
                var prom = videoEl.play(); // Explicitly play the video
                if (prom) {
                    prom.catch(function() {})
                }
                PremiumJpControls.callOnClickFullScreen(function(el) {
                    videoEl.muted = false;
                    Premium.expand.expand("expanded.html", "width:100%;height:100%")
                })

                break;

            case "body_left":
                // LEFT ---------------------------------------------------------------
                var videoEl = document.querySelector("VIDEO");
                var videoContainerEl = document.getElementById("video-container");

                function switchHandlerLeft(pos) {
                    Premium.utils.debug("switchHandler " + pos)
                }
                Premium.video.switchOnScroll(videoContainerEl, 3, switchHandlerLeft);
                Premium.video.sync(videoEl, 1);
                videoEl.volume = 0; // set volume to zero in all but one of the panels involved in syncing
                var prom = videoEl.play(); // Explicitly play the video
                if (prom) {
                    prom.catch(function() {})
                }
                PremiumJpControls.callOnClickFullScreen(function(el) {
                    videoEl.muted = false;
                    Premium.expand.expand("expanded.html", "width:100%;height:100%")
                })
                break;

            case "body_right":
                // RIGHT ---------------------------------------------------------------
                var videoEl = document.querySelector("VIDEO");
                var videoContainerEl = document.getElementById("video-container");

                function switchHandlerRight(pos) {
                    Premium.utils.debug("switchHandler " + pos)
                }
                Premium.video.switchOnScroll(videoContainerEl, 3, switchHandlerRight);
                Premium.video.sync(videoEl, 1);
                videoEl.volume = 0; // set volume to zero in all but one of the panels involved in syncin
                var prom = videoEl.play(); // Explicitly play the video
                if (prom) {
                    prom.catch(function() {})
                }
                PremiumJpControls.callOnClickFullScreen(function(el) {
                    videoEl.muted = false;
                    Premium.expand.expand("expanded.html", "width:100%;height:100%")
                })
                break;

            case "body_expanded":
                var videoEl = document.querySelector("VIDEO");

                Premium.video.sync(videoEl, undefined, Premium.video.SyncType_Get);
                videoEl.volume = 0; // set volume to zero in all but one of the panels involved in syncing
                videoEl.addEventListener("playing", function() {
                    setTimeout(function() {
                        document.body.className += " fade-in";
                    }, 100);
                }, { once: true });
                var prom = videoEl.play(); // Explicitly play the video
                if (prom) {
                    prom.catch(function() {})
                }

                // Safari fix
                document.body.addEventListener("click", function(e){
                  if (e.target.className && (e.target.className.indexOf("jp-controls-play") > -1 || e.target.className.indexOf("jp-controls-bigplay") > -1)){
                    if (!videoEl.paused){
                      setTimeout(function(){
                        videoEl.play();
                      }, 1000)
                      
                    }
                  }
                })

                break;

        }
        /* END OF CUSTOM JS */
    }
}